package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.PipeBaseQuery;
import com.deyuanyun.pic.pipe.controller.dto.PipeLineStatisticsDTO;
import com.deyuanyun.pic.pipe.controller.dto.PipeSegmentInfoQuery;
import com.deyuanyun.pic.pipe.controller.vo.PipeSegmentInfoVO;
import com.deyuanyun.pic.pipe.domain.PipeLineElement;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “管道基本信息”控制类
 *
 * @author tkc
 * 
 */
@Controller
@RequestMapping("/pipe/pipeLineInfo")
public class PipeLineInfoController extends PipeBaseController {

	@Resource
	private PipeMgrService pipeMgrService;

	/**
	 * 跳转到 管道基本信息 主页面
	 * @return
	 */
	@RequestMapping("/toPipeLineInfo.html")
	public ModelAndView toPipeLineInfo(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/basedata/pipe/pipeBaseInfo");
		return mv;
	}

	/**
	 * 根据条件分页查询 水工保护 数组对象
	 *
	 * @param pipeSegmentInfoQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(PipeSegmentInfoQuery pipeSegmentInfoQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		jsonBean.setList(this.queryVO(pipeSegmentInfoQuery));
		AjaxSupport.sendSuccessText("success", jsonBean);
	}

	/**
	 * 根据条件导出 水工保护
	 *
	 * @param pipeSegmentInfoQuery
	 * @throws IOException
	 */
	@RequestMapping("/export.json")
	@ResponseBody
	public void export(PipeSegmentInfoQuery pipeSegmentInfoQuery) throws IOException {
		List list = this.queryVO(pipeSegmentInfoQuery);
		PipeExportEnum exportEnum = PipeExportEnum.PipeLineInfo;
		String[] columnKeys = {"pipeLineName", "pipeSegmentName", "fromPipeValveRoomName", "toPipeValveRoomName", "pipelineType",
				"inserviceTime", "operationalStatus", "designLength", "countLength", "diameter",
				"wallthickness", "material", "coatingmaterial", "manupipeType", "coatingGrade",
				"jointMaterial", "incoatingMaterial", "cpType", "designTranYear","pressureRating",
				"mediumType", "dryOrWet", "h2s", "pipeFunction","designUnit",
				"construcion", "supervision","testingCompany", "recorder", "recordTime", "remark"};
		DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
	}

	private List queryVO(PipeSegmentInfoQuery pipeSegmentInfoQuery) {
		return new PipeSegmentInfoVO().fromDomainList(pipeMgrService.queryPipeSegmentInfo(pipeSegmentInfoQuery));
	}

	/**
	 * 跳转到 管道高层统计 主页面
	 *
	 * @param pipeLineStatisticsDTO
	 * @return
     */
	@RequestMapping("/toPipeStatistics.html")
	public ModelAndView toPipeStatistics(PipeLineStatisticsDTO pipeLineStatisticsDTO){
		ModelAndView mv = new ModelAndView();
		PipeLineElement beginElement = pipeMgrService.findPipeLineElement(pipeLineStatisticsDTO.getBeginId());
		PipeLineElement endElement = pipeMgrService.findPipeLineElement(pipeLineStatisticsDTO.getEndId());
		mv.addObject(pipeLineStatisticsDTO);
		mv.addObject("beginMileage", beginElement==null ? 0 : beginElement.getMileage());
		mv.addObject("endMileage", endElement==null ? 0 : endElement.getMileage());
		mv.setViewName("/basedata/pipe/pipeStatistics");
		return mv;
	}

	/**
	 * 根据条件 统计管道高层
	 *
	 * @param pipeBaseQuery
	 */
	@RequestMapping("/statistics.json")
	@ResponseBody
	public void statistics(PipeBaseQuery pipeBaseQuery) {

		AjaxSupport.sendSuccessText("success", pipeMgrService.statistics(pipeBaseQuery));
	}
}
